//package com.bjb.vr.schedule.service;
//
//import com.bjb.vr.common.ao.UpgradeAo;
//import com.bjb.vr.common.utils.DateTimeUtil;
//import com.bjb.vr.schedule.listener.Event;
//import com.bjb.vr.schedule.listener.LinuxUpgradeListener;
//import com.bjb.vr.schedule.listener.UpgradeProcess;
//import com.bjb.vr.schedule.listener.WindowsUpgradeListener;
//import com.bjb.vr.schedule.utils.OkHttpUtil;
//import lombok.extern.slf4j.Slf4j;
//import okhttp3.Call;
//import org.springframework.stereotype.Service;
//
///**
// * 服务升级逻辑处理类
// *
// * @version 1.0.0
// * @author: HuJingBo
// * @time: 2022/4/19 10:17
// */
//@Slf4j
//@Service
//public class UpgradeService {
//
//    private static final String OS = System.getProperties().getProperty("os.name");
//    private static final String WINDOWS = "Windows";
//    private static final String LINUX = "Linux";
//    private static final String SUCCESS = "true";
//    private static final String FAILED = "false";
//
//    /**
//     * 升级
//     *
//     * @param upgradeAo
//     * @return
//     */
//    public void upgrade(UpgradeAo upgradeAo) {
//        // 判断是否是windows系统
//        if (OS.equalsIgnoreCase(WINDOWS)) {
//            UpgradeProcess.builder().upgradeAo(upgradeAo).upgradeListener(new WindowsUpgradeListener()).build().start();
//        } else if (OS.equalsIgnoreCase(LINUX)) {
//            UpgradeProcess.builder().upgradeAo(upgradeAo).upgradeListener(new LinuxUpgradeListener()).build().start();
//        } else {
//            log.error("不支持的系统类型");
//            Event event = new Event();
//            event.setUpgradeAo(upgradeAo);
//            notifyEvent(event, FAILED, "不支持的系统类型");
//        }
//    }
//
//    /**
//     * 发送通知消息
//     *
//     * @param event
//     * @param result
//     * @param message
//     */
//    private void notifyEvent(Event event, String result, String message) {
//        OkHttpUtil.builder()
//                .url(event.getUpgradeAo().getNotifyUrl())
//                .addHeader("Content-Type", "application/json; charset=utf-8")
//                .addParam("success", result)
//                .addParam("message", message)
//                .addParam("process", event.getProgress())
//                .addParam("code", result.equals(SUCCESS) ? "200" : "500")
//                .addParam("timestamp", DateTimeUtil.nowStrTime())
//                .post(true)
//                .async(new OkHttpUtil.ICallBack() {
//                    @Override
//                    public void onSuccessful(Call call, String data) {
//                        log.debug("通知远程服务器成功！");
//                    }
//
//                    @Override
//                    public void onFailure(Call call, String errorMsg) {
//                        String url = call.request().url().toString();
//                        String method = call.request().method();
//                        log.error("通知远程服务器失败！[{}] {}", method, url);
//                    }
//                });
//    }
//}
